//
//  TMCache+TimeStamp.m
//  DeyiLife
//
//  Created by zxin on 15/6/30.
//  Copyright (c) 2015年 Deyi. All rights reserved.
//

#import "TMCache+TimeStamp.h"

@implementation TMCache (TimeStamp)

- (void)setObjectWithTimeStamp:(id <NSCoding>)object forKey:(NSString *)key block:(TMCacheObjectBlock)block {
    NSString *timeKey = [key stringByAppendingString:@"TIMESTAMP"];
    [[TMCache sharedCache] setObject:[NSDate date] forKey:timeKey block:block];
    [[TMCache sharedCache] setObject:object forKey:key block:block];
}

- (BOOL)objectForKeyIsExpire:(NSString *)key expireTime:(double)duration {
    BOOL isExpire = NO;
    NSString *timeKey = [key stringByAppendingString:@"TIMESTAMP"];
    NSDate *oldDate = [[TMCache sharedCache] objectForKey:timeKey];

    if(oldDate == nil) { //need fresh data
        return  YES;

    }
    NSTimeInterval time = [[NSDate date] timeIntervalSinceDate:oldDate];
    NSLog(@"objectForKeyIsTimeExpire %f",time);
    if (time > duration ){
        isExpire = YES;
    }
    return isExpire;
}

@end
